<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>java提高篇(五) 使用序列化实现对象的拷贝 | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="java基础">
    <meta name="description" content="学习来源：http://cmsblogs.com参考资料：http://www.cnblogs.com/DreamDrive/p/4005966.html 浅拷贝问题我们知道在Java中存在Cloneable这个接口，实现该接口的类都会具备被拷贝的能力，同时拷贝是在内存中进行的，在性能方面比我们直接通过new生成对象来的快，特别是在大对象的生成上，使得性能的提升非常明显。但是我们需要注意浅拷贝中对">
<meta name="keywords" content="java基础">
<meta property="og:type" content="article">
<meta property="og:title" content="java提高篇(五) 使用序列化实现对象的拷贝">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="学习来源：http://cmsblogs.com参考资料：http://www.cnblogs.com/DreamDrive/p/4005966.html 浅拷贝问题我们知道在Java中存在Cloneable这个接口，实现该接口的类都会具备被拷贝的能力，同时拷贝是在内存中进行的，在性能方面比我们直接通过new生成对象来的快，特别是在大对象的生成上，使得性能的提升非常明显。但是我们需要注意浅拷贝中对">
<meta property="og:updated_time" content="2017-12-05T09:58:21.697Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="java提高篇(五) 使用序列化实现对象的拷贝">
<meta name="twitter:description" content="学习来源：http://cmsblogs.com参考资料：http://www.cnblogs.com/DreamDrive/p/4005966.html 浅拷贝问题我们知道在Java中存在Cloneable这个接口，实现该接口的类都会具备被拷贝的能力，同时拷贝是在内存中进行的，在性能方面比我们直接通过new生成对象来的快，特别是在大对象的生成上，使得性能的提升非常明显。但是我们需要注意浅拷贝中对">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">java提高篇(五) 使用序列化实现对象的拷贝</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">java提高篇(五) 使用序列化实现对象的拷贝</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-11-09T02:03:00.000Z" itemprop="datePublished" class="page-time">
  2017-11-09
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#浅拷贝问题"><span class="post-toc-number">1.</span> <span class="post-toc-text">浅拷贝问题</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#利用序列化实现对象的拷贝"><span class="post-toc-number">2.</span> <span class="post-toc-text">利用序列化实现对象的拷贝</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-java_enhance5_使用序列化实现对象的拷贝"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">java提高篇(五) 使用序列化实现对象的拷贝</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年11月09日 10:03" datetime="2017-11-09T02:03:00.000Z"  itemprop="datePublished">2017-11-09</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>学习来源：<a href="http://cmsblogs.com" target="_blank" rel="external">http://cmsblogs.com</a><br>参考资料：<a href="http://www.cnblogs.com/DreamDrive/p/4005966.html" target="_blank" rel="external">http://www.cnblogs.com/DreamDrive/p/4005966.html</a></p>
<h2 id="浅拷贝问题"><a href="#浅拷贝问题" class="headerlink" title="浅拷贝问题"></a>浅拷贝问题</h2><p>我们知道在Java中存在Cloneable这个接口，实现该接口的类都会具备被拷贝的能力，同时拷贝是在内存中进行的，在性能方面比我们直接通过new生成对象来的快，特别是在大对象的生成上，使得性能的提升非常明显。但是我们需要注意浅拷贝中对象拷贝不彻底的问题，我们先看如下代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Cloneable</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> String name;</div><div class="line">    <span class="keyword">private</span> Email email;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> name;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> Email <span class="title">getEmail</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> email;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEmail</span><span class="params">(Email email)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.email = email;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">(String name, Email email)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>();</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">        <span class="keyword">this</span>.email = email;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> Person <span class="title">clone</span><span class="params">()</span> </span>&#123;</div><div class="line">        Person person = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            person = (Person) <span class="keyword">super</span>.clone();</div><div class="line">        &#125; <span class="keyword">catch</span> (CloneNotSupportedException e) &#123;</div><div class="line">            e.printStackTrace();</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> person;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">        <span class="comment">// 写封邮件</span></div><div class="line">        Email email = <span class="keyword">new</span> Email(<span class="string">"请参加会议"</span>,<span class="string">"请于今天12.30到会议室参加会议..."</span>);</div><div class="line">        Person person1 = <span class="keyword">new</span> Person(<span class="string">"张三"</span>, email);</div><div class="line">        Person person2 = person1.clone();</div><div class="line">        person2.setName(<span class="string">"李四"</span>);</div><div class="line">        Person person3 = person1.clone();</div><div class="line">        person3.setName(<span class="string">"王五"</span>);</div><div class="line">        </div><div class="line">        System.out.println(person1.getName()+<span class="string">"的邮件内容是："</span>+person1.getEmail().getContent());</div><div class="line">        System.out.println(person2.getName()+<span class="string">"的邮件内容是："</span>+person2.getEmail().getContent());</div><div class="line">        System.out.println(person3.getName()+<span class="string">"的邮件内容是："</span>+person3.getEmail().getContent());</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">/** output：</span></div><div class="line"><span class="comment"> * 张三的邮件内容是：请于今天12.30到会议室参加会议...</span></div><div class="line"><span class="comment"> * 李四的邮件内容是：请于今天12.30到会议室参加会议...</span></div><div class="line"><span class="comment"> * 王五的邮件内容是：请于今天12.30到会议室参加会议...</span></div><div class="line"><span class="comment"> */</span></div></pre></td></tr></table></figure></p>
<p>在该应用程序中，首先定义了一封邮件，然后将该邮件发送给张三，李四，王五。由于他们使用的是相同的邮件，仅有名字不同，所以使用张三对象拷贝出李四，王五对象然后更改名字即可。程序到这里都没有错，但是如果我们希望张三提前30分钟到场，即做如下修改：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">        <span class="comment">// 写封邮件</span></div><div class="line">        Email email = <span class="keyword">new</span> Email(<span class="string">"请参加会议"</span>,<span class="string">"请于今天12.30到会议室参加会议..."</span>);</div><div class="line">        Person person1 = <span class="keyword">new</span> Person(<span class="string">"张三"</span>, email);</div><div class="line">        Person person2 = person1.clone();</div><div class="line">        person2.setName(<span class="string">"李四"</span>);</div><div class="line">        Person person3 = person1.clone();</div><div class="line">        person3.setName(<span class="string">"王五"</span>);</div><div class="line">        </div><div class="line">        person1.getEmail().setContent(<span class="string">"请于今天12.00到会议室参加会议..."</span>);;</div><div class="line">        </div><div class="line">        System.out.println(person1.getName()+<span class="string">"的邮件内容是："</span>+person1.getEmail().getContent());</div><div class="line">        System.out.println(person2.getName()+<span class="string">"的邮件内容是："</span>+person2.getEmail().getContent());</div><div class="line">        System.out.println(person3.getName()+<span class="string">"的邮件内容是："</span>+person3.getEmail().getContent());</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">/**</span></div><div class="line"><span class="comment"> * 张三的邮件内容是：请于今天12.00到会议室参加会议...</span></div><div class="line"><span class="comment"> * 李四的邮件内容是：请于今天12.00到会议室参加会议...</span></div><div class="line"><span class="comment"> * 王五的邮件内容是：请于今天12.00到会议室参加会议...</span></div><div class="line"><span class="comment"> */</span></div></pre></td></tr></table></figure></p>
<p>到这里浅拷贝问题就出来了，为什么李四和王五的邮件内容也发生改变了呢？其实问题就出在clone方法上，我们知道该clone方法是使用Object类的clone方法，但是该方法存在一个缺陷，它并不会将对象的所有属性全部拷贝过来，而是有选择性的拷贝，基本规则如下：</p>
<ul>
<li>基本类型：拷贝其值</li>
<li>对象：拷贝其地址引用</li>
<li>String字符串：拷贝其地址引用，但是在修改时，会从字符串池中重新生成一个新的字符串，原有字符串对象保持不变。</li>
</ul>
<p>基于上面的规则我们很容易发现问题所在，他们三者都共用了一个对象，才会出现这种情况。对于这种情况我们很好解决，只需要在clone方法中新建一个对象，然后张三引用该对象即可。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">protected</span> Person <span class="title">clone</span><span class="params">()</span> </span>&#123;</div><div class="line">    Person person = <span class="keyword">null</span>;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">        person = (Person) <span class="keyword">super</span>.clone();</div><div class="line">        person.setEmail(<span class="keyword">new</span> Email(person.getEmail().getTitle(), person.getEmail().getContent()));</div><div class="line">    &#125; <span class="keyword">catch</span> (CloneNotSupportedException e) &#123;</div><div class="line">        e.printStackTrace();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> person;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>所以<strong>浅拷贝只是Java提供的一种简单拷贝机制，不便于直接使用。</strong></p>
<p>对于上面的解决方案还是存在一个问题，若我们系统中存在大量对象时通过拷贝生成的，如果我们每个类都写一个clone方法，并且需要进行深拷贝，新建大量的对象，这个工程时非常大的，这里我们可以利用序列化来实现对象的拷贝。</p>
<h2 id="利用序列化实现对象的拷贝"><a href="#利用序列化实现对象的拷贝" class="headerlink" title="利用序列化实现对象的拷贝"></a>利用序列化实现对象的拷贝</h2><p>如何利用序列化来完成对象的拷贝呢？在内存中通过字节流拷贝是比较容易实现的。把目对象写入到一个字节流中，再从字节流中将其读出来，这样就可以创建一个新的对象了，并且新对象与母对象之间并不存在引用共享的问题，真正实现对象的深拷贝。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CloneUtils</span> </span>&#123;</div><div class="line">    <span class="meta">@SuppressWarnings</span>(<span class="string">"unchecked"</span>)</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> &lt;T extends Serializable&gt; <span class="function">T <span class="title">clone</span><span class="params">(T obj)</span> </span>&#123;</div><div class="line">        T cloneObj = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">try</span> &#123;</div><div class="line">            <span class="comment">// 写入字节流</span></div><div class="line">            ByteArrayOutputStream out = <span class="keyword">new</span> ByteArrayOutputStream();</div><div class="line">            ObjectOutputStream obs = <span class="keyword">new</span> ObjectOutputStream(out);</div><div class="line">            obs.writeObject(obj);</div><div class="line">            obs.close();</div><div class="line">            <span class="comment">// 分配内存，写入原始对象，生成新对象</span></div><div class="line">            ByteArrayInputStream input = <span class="keyword">new</span> ByteArrayInputStream(out.toByteArray());</div><div class="line">            ObjectInputStream obsInput = <span class="keyword">new</span> ObjectInputStream(input);</div><div class="line">            cloneObj = (T) obsInput.readObject();</div><div class="line">            obsInput.close();</div><div class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</div><div class="line">            e.printStackTrace();</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> cloneObj;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>使用该工具类的对象必须实现Serializable接口，否则是没有办法实现克隆的。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = -<span class="number">7483706522682503036L</span>;</div><div class="line">    <span class="comment">// ...</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Email</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">2344831977081277392L</span>;</div><div class="line">    <span class="comment">// ...</span></div></pre></td></tr></table></figure></p>
<p>使用该工具类的对象只要实现Serializable接口就可以实现对象的克隆，无需继承Cloneable接口实现clone方法。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">        <span class="comment">// 写封邮件</span></div><div class="line">        Email email = <span class="keyword">new</span> Email(<span class="string">"请参加会议"</span>,<span class="string">"请于今天12.30到会议室参加会议..."</span>);</div><div class="line">        Person person1 = <span class="keyword">new</span> Person(<span class="string">"张三"</span>, email);</div><div class="line">        Person person2 = CloneUtils.clone(person1);</div><div class="line">        person2.setName(<span class="string">"李四"</span>);</div><div class="line">        Person person3 = CloneUtils.clone(person1);</div><div class="line">        person3.setName(<span class="string">"王五"</span>);</div><div class="line">        </div><div class="line">        person1.getEmail().setContent(<span class="string">"请于今天12.00到会议室参加会议..."</span>);;</div><div class="line">        </div><div class="line">        System.out.println(person1.getName()+<span class="string">"的邮件内容是："</span>+person1.getEmail().getContent());</div><div class="line">        System.out.println(person2.getName()+<span class="string">"的邮件内容是："</span>+person2.getEmail().getContent());</div><div class="line">        System.out.println(person3.getName()+<span class="string">"的邮件内容是："</span>+person3.getEmail().getContent());</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">/**</span></div><div class="line"><span class="comment"> * 张三的邮件内容是：请于今天12.00到会议室参加会议...</span></div><div class="line"><span class="comment"> * 李四的邮件内容是：请于今天12.30到会议室参加会议...</span></div><div class="line"><span class="comment"> * 王五的邮件内容是：请于今天12.30到会议室参加会议...</span></div><div class="line"><span class="comment"> */</span></div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:58:21.697Z" itemprop="dateUpdated">2017年12月5日 17:58</time>
</span><br>


        如要转载请注明出处：<a href="/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java基础/">java基础</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&title=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&title=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/11/13/Metrics/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Metrics-JVM上的实时监控类库</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/11/07/java_enhance4_四舍五入非小事/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">java提高篇(四) 四舍五入非小事</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&title=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&title=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《java提高篇(五) 使用序列化实现对象的拷贝》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/11/09/java_enhance5_使用序列化实现对象的拷贝/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
